<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<meta name="author" content="wangzhichao">
	<title>采购单任务管理</title>
	<script language="javascript" src="../script/common.js" type="text/javascript"></script>
	<script language="javascript" type="text/javascript">
	//全局变量
	var qt = {};				//数据表格对象
	var qt2 = {};				//数据表格对象2
	var billId="";				//采购单ID
	var type="";					//钻石类型
	var titlename="";				//标题名称
	var cOrg = Common.cOrg();				//取当前地址
	var detail_editable;
	var selectedOrg;
	var groupOpts = {
        	orgLevel:cOrg.orgLevel,
			orgId : cOrg.id
        }
	var parseBackValue = function(row){
			var obj = {}
       		obj.value = row[0]
       		obj.text = row[5]
       		return obj
       	}
	//页面控件初始化
	function load(){
		Eui.showNavigation();//导航
		setPages(["listview","detview","listview1"])
		cOrg = Common.cOrg()
		//初始化申请单状态("新建","申请","通过","拒绝","确认")

		var statusList
		if(cOrg.orgLevel == Constants.ORG_LEVEL_HQ){
			 statusList = Common.getSubsetItems("PurchaseBillStatus", [Constants.PurchaseBillStatus_NEW, Constants.PurchaseBillStatus_FINISH]);
		}else{
			 statusList = Common.getSubsetItems("PurchaseBillStatus", [Constants.PurchaseBillStatus_NEW, Constants.PurchaseBillStatus_ACCPECTED]);
		}
//      searchForm.currentOrg.value = cOrg.id;
        setOrg(cOrg);
		billTypeList = Common.getSubsetItems("PurchaseBillType", [1, 2]); 	
		Common.initDict(searchForm.type,billTypeList,"全 部");	
		Common.initDict(searchForm.diamondType,"diamondType","全 部");
		Eui.initBeginDateInput("beginDateTd","创建日期开始","searchForm","startrequestDate","endrequestDate","date");
		Eui.initEndDateInput("endDateTd","创建日期结束","searchForm","startrequestDate","endrequestDate","date");
//		Common.initMultiSelect("diamond_group_td","添加钻石","demandform","groups","/aita/sel_diamond.html",460,10,groupOpts,parseBackValue);
		createTable();
	}
	
	//生成查询语句
	function createTable(){
		var columns = [];
		columns[0] = {type:"checkbox"};
		columns[1] = {render:numFunc};
		columns[3] = {defOrder:true}
		var discolumns;
		var xmlShow = "manage_pbill_query";
		
		if(cOrg.orgLevel == Constants.ORG_LEVEL_HQ){
			columns[14] = {title:"操作",width:12,render:operationFunc,align:"center",order:false};
			discolumns = [0,1,2,3,4,5,6,7,8,9,10,14]
			titlename = "采购单列表"
		}else{
			columns[14] = {title:"操作",width:12,render:operationFunc,align:"center",order:false};
			discolumns = [0,1,2,3,4,5,6,7,8,9,10,14]
			titlename = "采购单列表"
		}
		searchForm.purchaseBillStatus.value = Constants.PurchaseBillStatus_NEW	
		qt = new QueryTable(xmlShow,"searchForm","datalist",columns,{
			displayColumns:discolumns,
			title:titlename,
//			paramFunc: function(){
//				return {
//					purchaseBillStatus:Constants.PurchaseBillStatus_NEW
//				};
//			},
			buttons:[
//						{value:"导 入",actions:onImport},
						{value:"增 加",actions:onEditP},
						{value:"删 除",actions:onDelete}						
					]
		});
	}
	//数据处理方法
	function numFunc(v){
		return "<a href=# onclick=showDetail('"+v+"')>"+v+"</a>"
	}
	
	//处理数据 点id进入编辑页面
	function operationFunc(v,r){
		var html="<div style='color:gray'>";
		if(cOrg.orgLevel == Constants.ORG_LEVEL_HQ){
			//<column field="o.purchaseBillStatus" hidden="true" />
			var status = r[7];
			if(status==Constants.PurchaseBillStatus_NEW)
				html += " <a href=# onclick=onEditP('"+r[1]+"',"+r[1]+")>修改</a> <a href=# onclick=quash4('"+r[0]+"')>删除</a> <a href=# onclick=\"confirmCard('"+r[0]+"','"+r[4]+"');return false;\">确认</a>";
		}else{
			var status = r[7];
//			if(status==Constants.PurchaseBillStatus_NEW)
//				html += " <a href=# onclick=onEditP('"+r[1]+"',"+r[1]+")>修改</a> <a href=# onclick=quash4('"+r[0]+"')>删除</a>";
		}
		return html;
	}
	//显示静态明细
	function showDetail(id){
		if (id != null){
			billId=id
			requestframe.document.location.href = "purchase_bill_detail.html?id="+id;
			switchPage("listview1")
		}
	}

	//确认制卡
  	function confirmCard(id,taskAmount){
		if(taskAmount <= 0){
			showInformation("数量小于或等于0时不可进行确认操作!");
			return;
		}
		DWR.call('purchaseBillService.confirmPurchaseBill',id,taskAmount,function(v){
			if(v){
				showInformation("确认成功!")
		 		qt.showTable();
			}
         });
		
    }
	function addDiamond(){
		if(!demandform.uniqueCode.value){
			showInformation("采购单未保存，不能增加钻石")
			return;
		}
		var v = openDialogFrame("/aita/diamond.html?id="+billId,'增加钻石',550,750);
		if (v){
		}
	}
	function delDiamond(){
		
	}
	
	//删除采购单
	function quash4(id){
		if (showQuestion("是否删除采购单？")){
			DWR.call("purchaseBillService.deleteBill",id,function(flag){
				if(flag){
					showInformation("采购单删除成功")
				}
				switchPage("listview");
				 qt.showTable();
			});
		}else
			return;
		
		
	}
	//修改新建制卡申请单
	function onSaveBill(){
		if(!checkForm(demandform))return;
		var apply = Common.getValues(demandform);
		var groups = ""
//        var select = demandform.groups
//        for (var i=0;i<select.childNodes.length;i++){
//			var op = select.childNodes[i];
//			if (!op.value) continue;
//			if (groups) groups += ","
//			groups += op.value;
//        }
		DWR.call("purchaseBillService.addBill",apply,groups,function(showapply){
		 	if (detail_editable) {
				showInformation("修改采购单成功!");
			}else{
				showInformation("新增采购单成功!");
			}
		 	backShow();
		},{disCtrl:$('saveBtn')});
	}
	//页面刷新返回
	function backShow(){
		$("orderIdTd").style.display="none"
		diamonddatalist.style.display="none"
		switchPage('listview')
		qt.showTable();
	}
	//无页面刷新返回
	function backShow1(){
		$("orderIdTd").style.display="none"
		$("diamond_import_td").style.display="none"
//		$("diamond_group_td").style.display=""
		$("diamond_import_td").style.display=""
		switchPage('listview')
	}
	//校验时间和显示类表数据
   function selectDate(){
		searchForm.organization.value = cOrg.id;
		qt.showTable();
   }
   //点击选择卡片方法
   function seldiamondType(){
	   	var v = openDialogFrame("/basic/card_type_list.html",'选择钻石类型',464,700)
	    if (v){
		    searchForm.diamondType.value=v[1];
		}
   }
   function onNew(){
		type = demandform.diamondType.value;
		var isPersent = false;
		var v = openDialogFrame("/store/sel_req_order.html?ct="+type+"&isPersent="+isPersent,"选择订购单",450,700);
		if (v){
			demandform.amount.value = v;
		}
	}
	
	//导入采购钻石
	function onImport(){
		if(!demandform.uniqueCode.value){
			showInformation("采购单未保存，不能导入采购钻石")
			return;
		}
		var options = {}
		options.headFile = "diamondForPurchaseBill";
		var url = spellUrl("../common/import_data.html",options);
		v=openDialogFrame(url,"导入采购钻石",600,800);
		createTable2();
	}
	function onEditP(id) {
		detail_editable = false;
		demandform.diamondType.disabled = false;
		demandform.type.disabled = false;
		Common.initDict(demandform.diamondType,"diamondType")
//		var billTypeList1 = Common.getSubsetItems("PurchaseBillType", [1]); 	
		Common.initDict(demandform.type,billTypeList)
		Common.initDict("certificateType","certificateType");
		$("demandform").reset();
		switchPage('detview');
		diamonddatalist.style.display="none"
		billId=id
//		Common.initMultiSelect("diamond_group_td","添加钻石","demandform","groups","/aita/sel_diamond.html",460,10,groupOpts,parseBackValue);
		if(id!=null){
			searchForm2.uniqueCode.value=id;
			detail_editable = true;
			demandform.uniqueCode.disabled = true;
			demandform.diamondType.disabled = true;
			demandform.type.disabled = true;
			DWR.call("purchaseBillService.getPurchaseBill",id,function(apply){
					apply[0].diamondType = Common.getText("diamondType",apply[0].diamondType);
					Common.setValues(demandform,apply[0]);
					typeChange(demandform.type);
					demandform.uniqueCode.value = apply[0].uniqueCode;
					if(demandform.type.value==2){
						demandform.uniqueCode.disabled = false;
						demandform.order.value = apply[4];
						demandform.discount.value = apply[5].discount;
						demandform.costPrice.value = apply[5].costPrice;
						demandform.certificateType.value = apply[5].certificateType;
					}
				
//					initGroup(apply[3])
				  	switchPage("detview");
			})
			createTable2(id)
		}
	}
	
	
	
	function initGroup(diamondIds){
		var ids = diamondIds.split(",")
		var select = demandform.groups
		for (var i=0;i<ids.length;i++){
			Common.addOption(select,ids[i],ids[i]);
        }
	}
	
	function clearGroup(select){
			Common.removeOption(select)
	}
	//数据删除方法
	function onDelete() {
		var ids = qt.getBoxValue();
		Eui.onDel("purchaseBillService.deleteBills",ids, "采购单", null, function() {
			qt.showTable();
		});
	}				
	//选择机构方法
		function selectOrganization(b){
			var opts = {funcType : Constants.SELORG_ALL} //获得直属子节点
		
			if (b){
				SystemMgnt.selOrg(setSearchOrg,opts);
			}else{
				SystemMgnt.selOrg(setOrg,opts);
			}
		}
		
        //searchform 选择机构 回调方法 
		function setSearchOrg(sorg){	
			searchForm.orgName.value = sorg.name;
			searchForm.organization.value = sorg.id;
		}
		
        //选择机构 回调方法
        function setOrg(sorg) {
        	selectedOrg = sorg;
            searchForm.reset();
            searchForm.organization.value = sorg.id;
            searchForm.orgName.value = sorg.name;
        }
	//选择操作员
        function selOper() {
			SystemMgnt.selOperator(function(v) {
				if (!v) return;
				searchForm.operatorName.value = v.name;
//				searchForm.member.value = v.id;
			}, {orgId:selectedOrg.id})
		}
		
	//选择订单
	function selOrder(){
		openDialogFrame("/aita/sel_order.html","选择订购单",500,700,function(v){
			if (!v) return;
			demandform.order.value = v.id;
		})
	}
		
	function createTable2(id){
//		var columns = [];
		diamonddatalist.style.display=""
//		columns[0] = {type:"checkbox"};
		qt2 = new QueryTable("Diamond_caigou_import","searchForm2","diamonddatalist",'',{
			exportable:true
//			buttons:[
//				{value:"增 加",actions:addDiamond},
//				{value:"删 除",actions:delDiamond}						
//			]
		});
		qt2.showTable();
	}
	
	function typeChange(type){
		demandform.order.value=""
		if (type.value==2){
			$("orderIdTd").style.display="";
			$("diamondInfo").style.display="";
//			$("diamond_group_td").style.display="none";
			$("diamond_import_td").style.display="none";
			
		}else{
			$("orderIdTd").style.display="none";
			$("diamondInfo").style.display="none";
//			$("diamond_group_td").style.display=""
			$("diamond_import_td").style.display=""
		}
	}
					
	
	</script>
</head>

<body onload="load()">
	<!--页面导航-->
	<div class="nav"><div id="nav">&nbsp;</div></div>
	<!--查询静态页面部分-->
		<div id="listview" >	
			<!--查询条件form-->
			<div class="searchForm">
				<form id="searchForm" name="searchForm">
					<input type="hidden" name="organization" >
					<input type="hidden" name="purchaseBillStatus" >
					<table cellSpacing=0 cellPadding=6 width="100%" border=0 class="col3 label8">
						<tr>
							<td><label>单据编号</label> <input type="text" name="requestid" onblur="checkV(this,'number')" maxlength=12 ></td>
							
							<td><label>采购类型</label> <select name="type"><option></option></select></td>
						</tr>
						<tr style="display:none">
						<td><label>钻石类型</label> <select name="diamondType"><option></option></select></td>
							
							<td class='label6'><label>所属机构</label> <input type="text" name="orgName" readonly class="readonly"  onclick="selectOrganization();">
							<a class=orga href=# onclick="selectOrganization();">&nbsp;&nbsp;</a></td>
                			<td class='label6'><label>操&nbsp;作&nbsp;员</label> <input type="text" name="operatorName" readonly class="readonly">
							<a class=orga href=# onclick="selOper();">&nbsp;&nbsp;</a></td>
							
							<td><button type="button" onclick="selectDate()" >查   询</button></td>
						</tr>
						<tr>
							<td id="beginDateTd"></td>
							<td id="endDateTd"></td>
							<td><button type="button" onclick="selectDate()" >查   询</button></td>
						</tr>
					</table>
				</form>
			</div>
				<!--查询结构列表-->
				<div id="datalist"></div>
		</div>
	<div class="listview1" id="listview1" style='display:none;'>
		<iframe id="requestframe" name="requestframe" style="width:100%;height:90%;overflow:auto;" frameborder=0 src=""></iframe>
	</div>
	
	
	<div id="detview" style="display:none;">
		<div>
		<form id="demandform" name="demandform">
				<input type="hidden" name="id">
				<input type="hidden" name="basediamondType">
				<div class="form">
					<div class="formTitle">新建采购单</div>
					<table cellSpacing=0 cellPadding=4 width="100%" border=0 class="col2 label8">
						<tr>
							<td>
								<label>单据编号</label> <input type="text" name="uniqueCode" onblur="checkV(this,'positiveInt',true)" maxlength=10><span class='des'>*</span>
							</td>
							<td>
								<label>需求数量</label> <input type='text' name='amount' onblur=checkV(this,'positiveInt',true) maxlength=9><span class='des'>*</span><span id="ismerge"></span>
							</td>
						</tr>
						<tr style="display:none">
							<td>
								<label>钻石类型</label>  <select name="diamondType" readonly class="readonly"><option></option></select>
							</td>
							<td>
								<label>完成日期</label> <input type=text name="completeDate" onblur="checkV(this,'futureDate');" ><a href="javascript:void(0)" onclick="showDatePicker($('demandform').completeDate);" class="date" title="选择日期">&nbsp;&nbsp;</a>
							</td>
						</tr>
						<tr>
							<td>
								<label>供&nbsp;应&nbsp;商</label> <input type='text' name='provider' id='provider' maxlength=64>
							</td>
							<td>
								<label>备&nbsp;&nbsp;&nbsp;&nbsp;注</label> <input type='text' name='comments' id='comments' maxlength=64>
							</td>
						</tr>
						<tr>
							<td>
								<label>采购类型</label>  <select name="type" readonly class="readonly" onchange="typeChange(this);"><option></option></select>
							</td>
							<td id="orderIdTd" style="display:none;">
								<label>订单编号</label> <input type="text" name="order" readonly class="readonly"><!--<a class=orga href=# onclick="selOrder();">&nbsp;&nbsp;</a>-->
							</td>
							<td id="diamond_import_td">
								<label></label>
								<button type='button' id='importBtn' name='importBtn' onclick='onImport();'>导入采购钻石</button>
							</td>		
						</tr>
						<tr>
							<!--td id="diamond_group_td" style="">
							</td-->
						</tr>
						
					</table>
					
				</div>
				
				<div class="form" id="diamondInfo" style="display:none">
					<div class="formTitle">订购钻石信息</div>
					<table cellSpacing=0 cellPadding=4 width="100%" border=0 class="col2 label8">
						<tr>
							<td>
								<label>折扣</label> <input type="text" name="discount" onblur="checkV(this,'positiveInt',true)" maxlength=10><span class='des'>*</span>
							</td>
							<td>
								<label>采购价格</label> <input type="text" name="costPrice" onblur="checkV(this,'positiveInt',true)" maxlength=10><span class='des'>*</span>
							</td>
						</tr>
						<tr>
							<td>
								<label>证书类型</label>  <select name="certificateType"><option></option></select><span class='des'>*</span>
							</td>
						</tr>									
					</table>
					
				</div>
				<p align="center">
					<button type='button' id='saveBtn' name='saveBtn' onclick='onSaveBill();'>保   存</button>&nbsp;&nbsp;&nbsp;<button type='button' onclick='backShow1()' >返   回</button>
				</p>
		</form>
		</div>
		<!--查询结构列表-->
		<div class="searchForm">
			<form id="searchForm2" name="searchForm2">
				<input type="hidden" id="uniqueCode" name="uniqueCode">
			</form>
		</div>
		<div id="diamonddatalist"></div>
	</div>
</body>
</html>